class Admin::PaymentsController < AdminController
  before_filter :find_payment, :only => [:show, :edit, :update, :destroy]

  # GET /payments
  # GET /payments.xml
  def index
    @payments = Payment.all

    respond_to do |wants|
      wants.html # index.html.erb
      wants.xml  { render :xml => @payments }
    end
  end

  # GET /payments/1
  # GET /payments/1.xml
  def show
    respond_to do |wants|
      wants.html # show.html.erb
      wants.xml  { render :xml => @payment }
    end
  end
  
  def show_remote_form
    @payment  = (params[:id] == "new" ? Payment.new : Payment.find(params[:id]))
    @project  = Payment.find params[:project_id]
    render :partial => "remote_form", :locals => {:payment => @payment, :project => @project}
  end

  # GET /payments/new
  # GET /payments/new.xml
  def new
    @payment = Payment.new

    respond_to do |wants|
      wants.html # new.html.erb
      wants.xml  { render :xml => @payment }
    end
  end

  # GET /payments/1/edit
  def edit
  end

  # POST /payments
  # POST /payments.xml
  def create
    @payment = Payment.new(params[:payment])

    if @payment.save
      respond_to do |wants|
        wants.js{
          render :update do |page|
            page.insert_html :bottom, 'project_payments', :partial => "admin/payments/show", :locals => {:payment => @payment, :project => @payment.project, :show_actions => true, :row_wrap => true}
            page << "$j('#payment_#{@payment.id.to_s}').effect('pulsate', { times:1 }, 500);"
            page << "$j('#payment_form_row').html(\"\")"
          end
        }
      end
    else
      render :action => 'new'
    end
  end

  # PUT /payments/1
  # PUT /payments/1.xml
  def update
    respond_to do |wants|
      if @payment.update_attributes(params[:payment])
        wants.js{
          render :update do |page|
            page.replace_html 'payment_' + @payment.id.to_s, :partial => "admin/payments/show", :locals => {:payment => @payment, :project => @payment.project, :show_actions => true, :row_wrap => false}
            page << "$j('#payment_#{@payment.id.to_s}').effect('pulsate', { times:1 }, 500);"
            page << "$j('#payment_form_row').html(\"\")"
          end
        }
      else
        wants.html { render :action => "edit" }
        wants.xml  { render :xml => @payment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /payments/1
  # DELETE /payments/1.xml
  def destroy
    element_id = @payment.id
    @payment.destroy

    respond_to do |wants|
      wants.js{
          render :update do |page|
            page << "$j(\"#payment_#{element_id}\").fadeOut();"
          end
        }
    end
  end

  private
    def find_payment
      @payment = Payment.find(params[:id])
    end

end
